package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 品牌表 业务实现类
 *
 * @author lan
 * @since 2023-04-18
 */
@Service
public class BaseTrademarkServiceImpl extends ServiceImpl<BaseTrademarkMapper, BaseTrademark> implements BaseTrademarkService {

    /**
     * 品牌分页查询
     *
     * @param trademarkPage
     * @return
     */
    @Autowired
    private BaseCategoryTrademarkService baseCategoryTrademarkService;
    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;
    @Override
    public IPage<BaseTrademark> getBaseTrademarkByPage(IPage<BaseTrademark> trademarkPage) {
        //根据修改时间排序
        LambdaQueryWrapper<BaseTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(BaseTrademark::getUpdateTime);

        return this.page(trademarkPage,queryWrapper);
    }

    /**
     * //根据category3Id获取品牌列表
     *
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getBaseCategoryTrademarkList(Long category3Id) {
        //1.根据分类ID查询 分类品牌关系表得到分类品牌集合
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
//        queryWrapper.select(BaseCategoryTrademark::getTrademarkId);
        List<BaseCategoryTrademark> list = baseCategoryTrademarkService.list(queryWrapper);
        //2.获取分类品牌集合中品牌ID集合
        if (!CollectionUtils.isEmpty(list)){
        List<Long> longs = list.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
        List<BaseTrademark> baseTrademarkList = this.listByIds(longs);
        return baseTrademarkList;
        }
        return null;
    }

    /**
     * 根据分类ID查询该分类未关联品牌
     *自定义sql语句,查询 未关联的品牌集合
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getCurrentTrademarkList(Long category3Id) {
        List<BaseTrademark> list= baseTrademarkMapper.findCurrentTrademarkList(category3Id);
        return list;
    }

    /**
     * 根据品牌ID查询品牌信息
     *
     * @param tmId
     * @return
     */
    @Override
    public BaseTrademark getTrademark(Long tmId) {
        BaseTrademark trademark = this.baseTrademarkMapper.selectById(tmId);
        return trademark;
    }
}
